﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LWrap.Entities;
namespace LWrap.Search
{
    public class StandardSearch:SearchBase
    {
        public override List<SearchResultList> ExecuteSearch()
        {
            if (base.IsValidSearch() == false)
                return null;

            SearchQueryParser queryMaker = new SearchQueryParser();
            Lucene.Net.Search.Query query = queryMaker.GetQuery(base.BooleanCondition, base.DefaultField);
            Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(base.IndexLocation);
            try
            {

                Lucene.Net.Search.Hits hits = searcher.Search(query);
                List<SearchResultList> searchResults = base.GetSearchResult(hits);
                return searchResults;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                searcher.Close();
            }
        }

        public override List<SearchResultList> ExecuteSearch(string booleanCondition)
        {
            base.BooleanCondition = booleanCondition;
            return this.ExecuteSearch();
        }

        public override List<SearchResultList> ExecuteSearch(string booleanCondition, string indexLocation)
        {
            base.BooleanCondition = booleanCondition;
            base.IndexLocation = indexLocation;
            return this.ExecuteSearch();
        }

        public override List<SearchResultList> ExecuteSearch(string booleanCondition, string indexLocation, List<string> fieldValueInSearchResult)
        {
            base.BooleanCondition = booleanCondition;
            base.IndexLocation = indexLocation;
            base.FieldValueInSearchResult = fieldValueInSearchResult;
            return this.ExecuteSearch();
        }
    }
}
